package com.woorigil.social.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.woorigil.social.controller.BaseController;
import com.woorigil.social.domain.point.DealPoint;
import com.woorigil.social.domain.point.DealPointCondition;
import com.woorigil.social.domain.point.DealPointDao;

@Controller
@RequestMapping("/dealPoint")
public class DealPointController extends BaseController {
	@Autowired DealPointDao dealPointDao;
	
	@RequestMapping("/form")
	public ModelAndView form(@RequestParam(defaultValue="0") int id) {
		DealPoint dealPoint = dealPointDao.select(id);
		ModelAndView mav = new ModelAndView("dealPoint/form");
		mav.addObject("dealPoint", dealPoint);
		return mav;
	}

	@RequestMapping("/insert")
	public ModelAndView insert(DealPoint dealPoint, HttpServletRequest request) throws Exception {
		int id = dealPointDao.insert(dealPoint);
		ModelAndView mav = new ModelAndView(getRedirectUrl("/dealPoint/view"));
		mav.addObject("id", id);
		return mav;
	}

	@RequestMapping("/update")
	public ModelAndView update(DealPoint dealPoint) throws Exception {
		dealPointDao.update(dealPoint);
		ModelAndView mav = new ModelAndView(getRedirectUrl("/dealPoint/view"));
		mav.addObject("dealId", dealPoint.getDealId());
		return mav;
	}
	
	@RequestMapping("/delete")
	public ModelAndView delete(int id) {
		dealPointDao.delete(id);
		ModelAndView mav = new ModelAndView(getRedirectUrl("/dealPoint/list"));
		return mav;
	}
	
	@RequestMapping("/view")
	public ModelAndView view(int id) {
		DealPoint dealPoint = dealPointDao.select(id);
		if (dealPoint == null) {
			throw new IllegalArgumentException("message");
		}
		ModelAndView mav = new ModelAndView("dealPoint/view");
		mav.addObject("dealPoint", dealPoint);
		return mav;
	}	
	
	@RequestMapping("/list")
	public ModelAndView list(DealPointCondition condition) {
		List<DealPoint> dealPointList = dealPointDao.search(condition);
		
		ModelAndView mav = new ModelAndView("dealPoint/list");
		mav.addObject("condition", condition);
		mav.addObject("dealPointList", dealPointList);
		return mav;
	}
}